<!-- 举报记录编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改举报记录':'添加举报记录'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item label="举报人编号:" prop="member_id">
        <el-input-number
          :min="0"
          v-model="form.member_id"
          placeholder="请输入举报人编号"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item label="举报分类:" prop="report_type">
        <el-input-number
          :min="0"
          v-model="form.report_type"
          placeholder="请输入举报分类"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item label="举报类型，1、帖子，2、评论:" prop="type">
        <el-input-number
          :min="0"
          v-model="form.type"
          placeholder="请输入举报类型，1、帖子，2、评论"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item label="举报的目标编号:" prop="target_id">
        <el-input-number
          :min="0"
          v-model="form.target_id"
          placeholder="请输入举报的目标编号"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item
        label="举报理由:"
        prop="reason">
        <el-input
          :maxlength="20"
          v-model="form.reason"
          placeholder="请输入举报理由"
          clearable/>
      </el-form-item>

      <el-form-item label="状态，0、未处理，1、核实符合，2、核实不符合:" prop="status">
        <el-input-number
          :min="0"
          v-model="form.status"
          placeholder="请输入状态，0、未处理，1、核实符合，2、核实不符合"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item label="处理人:" prop="deal_userid">
        <el-input-number
          :min="0"
          v-model="form.deal_userid"
          placeholder="请输入处理人"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item
        label="处理时间:"
        prop="deal_time">
        <el-input
          :maxlength="20"
          v-model="form.deal_time"
          placeholder="请输入处理时间"
          clearable/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'BoxReportEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        member_id: [
          {required: true, message: '请输入举报人编号', trigger: 'blur'}
        ],
        report_type: [
          {required: true, message: '请输入举报分类', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请输入举报类型，1、帖子，2、评论', trigger: 'blur'}
        ],
        target_id: [
          {required: true, message: '请输入举报的目标编号', trigger: 'blur'}
        ],
        reason: [
          {required: true, message: '请输入举报理由', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请输入状态，0、未处理，1、核实符合，2、核实不符合', trigger: 'blur'}
        ],
        deal_userid: [
          {required: true, message: '请输入处理人', trigger: 'blur'}
        ],
        deal_time: [
          {required: true, message: '请输入处理时间', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/boxreport/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
